package com.tg.app.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.tg.app.R;
import com.tg.appcommon.android.TGLog;
import com.tg.data.bean.DeviceItem;
import com.tg.app.util.LogUtils;

import java.util.List;

/**
 * Describe:
 * Created by Rance Rang on 2019-12-26.
 * <p>
 * Copyright (c) Tange.Inc All rights reserved.
 */
public class ViewPagerAdpater extends RecyclerView.Adapter<ViewPagerAdpater.ViewPagerHolder> {
    private static final String TAG = "ViewPagerAdapter";
    private List<DeviceItem> itemList;
    private Context context;
    private int mNumColumns = 2; //每页显示列数，默认为：2
    private int mPageSize = 4; //每页显示数量，默认为：4
    private int mPageCount = 0;

    private View.OnTouchListener onTouchListener;

    public void setOnTouchListener(View.OnTouchListener onTouchListener) {
        this.onTouchListener = onTouchListener;
    }

    public ViewPagerAdpater(Context context, List<DeviceItem> itemList) {
        this.context = context;
        this.itemList = itemList;

    }


    @NonNull
    @Override
    public ViewPagerAdpater.ViewPagerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewPagerHolder(LayoutInflater.from(context).inflate(R.layout.widget_recycler_view_pager, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewPagerAdpater.ViewPagerHolder holder, int position) {
        if (holder != null) {
            // 设置布局为GridView布局
            holder.rvPager.setLayoutManager(new GridLayoutManager(context, mNumColumns));
            holder.rvPager.setItemViewCacheSize(0);
            setGVAdapterOrRefresh(holder.rvPager, position);
            holder.rvPager.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (onTouchListener != null) {
                        onTouchListener.onTouch(v, event);
                    }
                    return false;
                }
            });
        }
    }

    @Override
    public void onViewDetachedFromWindow(@NonNull ViewPagerHolder holder) {
        super.onViewDetachedFromWindow(holder);
        TGLog.d(TAG, "SplitScreenPlayer  ViewPagerAdpater  onViewDetachedFromWindow ");
    }

    @Override
    public void onViewRecycled(@NonNull ViewPagerHolder holder) {
        super.onViewRecycled(holder);
        TGLog.d(TAG, "SplitScreenPlayer  ViewPagerAdpater  onViewRecycled ");
    }

    @Override
    public void onViewAttachedToWindow(@NonNull ViewPagerHolder holder) {
        super.onViewAttachedToWindow(holder);
        TGLog.d(TAG, "SplitScreenPlayer  ViewPagerAdpater  onViewAttachedToWindow ");
    }

    private void setGVAdapterOrRefresh(RecyclerView rv, int position) {
        RecyclerView.Adapter adapter = rv.getAdapter();
        // 如果已经设置过适配器，直接刷新即可
        if (null != adapter) {
            adapter.notifyDataSetChanged();
        } else {

            SinglePlayAdapter singlePlayAdapter = new SinglePlayAdapter(context,itemList);
            singlePlayAdapter.setIndex(position);
            singlePlayAdapter.setPageSize(mPageSize);
            singlePlayAdapter.setPageCount(mPageCount);
            rv.setAdapter(singlePlayAdapter);
        }
    }

    @Override
    public int getItemCount() {

        if (itemList != null) {
            // 计算ViewPager的页数
            mPageCount = itemList.size() % mPageSize == 0 ? itemList.size() / mPageSize : itemList.size() / mPageSize + 1;
        }
        return mPageCount;
    }

    public class ViewPagerHolder extends RecyclerView.ViewHolder {
        RecyclerView rvPager;

        public ViewPagerHolder(@NonNull View itemView) {
            super(itemView);
            rvPager = itemView.findViewById(R.id.rv_rvg_pager);

        }
    }

}
